package com.wenjh.jdmzt.android.view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.wenjh.jdmzt.R;

public class LoadingButton extends LinearLayout implements View.OnClickListener {
	private int layout = R.layout.common_btn_loadmore;
	private CharSequence labelText = "";
	private ProgressBar progressBar = null;
	private TextView labelView = null;
	private LinearLayout containerView = null;
	
	/** 平常状态下按钮的文字 */
	private String normalText = "查看更多";
	/** 按钮加载时的文字 */
	private String loadingText = "正在加载 ...";
	
	private boolean loading = false;
	
	private OnProcessListener processListener = null;
	
	public LoadingButton(Context context) {
		super(context);
		inflate();
	}
	
	public LoadingButton(Context context, int layoutResId) {
		super(context);
		layout = layoutResId;
		inflate();
	}
	
	public LoadingButton(Context context, AttributeSet attrs, int layoutResId) {
		super(context);
		layout = layoutResId;
		inflate();
	}
	
	public LoadingButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		inflate();
	}

	private void inflate() {
		LayoutInflater inflater = ((LayoutInflater) getContext()
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE));
		inflater.inflate(layout, this, true);

		progressBar = (ProgressBar) findViewById(R.id.btn_progress);
		labelView = (TextView) findViewById(R.id.btn_tv_text);
		containerView = (LinearLayout) findViewById(R.id.btn_layout_container);
		init();
	}
	
	@Override
	public void setBackgroundResource(int resid) {
		containerView.setBackgroundResource(resid);
	}
	
	@Override
	public void setBackgroundColor(int color) {
		containerView.setBackgroundColor(color);
	}
	
	@Override
	public void setBackgroundDrawable(Drawable d) {
		containerView.setBackgroundDrawable(d);
	}
	
	public void setTextColor(int color) {
		labelView.setTextColor(color);
	}
	
	public void setLayoutPadding(int left, int top, int right, int bottom) {
		containerView.setPadding(left, top, right, bottom);
	}


	public LoadingButton setText(CharSequence c) {
		if (c == null) {
			labelView.setVisibility(View.GONE);
		} else {
			this.labelText = c;
			labelView.setVisibility(View.VISIBLE);
			labelView.setText(this.labelText);
		}
		return this;
	}
	public LoadingButton setText(int resId) {
		if (resId <= 0) {
			labelView.setVisibility(View.GONE);
		} else {
			this.labelText = getContext().getString(resId);
			labelView.setVisibility(View.VISIBLE);
			labelView.setText(this.labelText);
		}
		return this;
	}

	public CharSequence getText() {
		return labelText;
	}
	
	public void setButtonOnClickListener(View.OnClickListener click) {
		containerView.setOnClickListener(click);
	}
	
	@Override
	public void setEnabled(boolean enabled) {
		containerView.setEnabled(enabled);
	}
	
	public void setNormalText(int resId) {
		setNormalText(getContext().getString(resId));
	}
	
	public void setLoadingText(int resId) {
		setLoadingText(getContext().getString(resId));
	}
	
	public void setNormalText(String text) {
		this.normalText = text;
		if(!loading) {
			setText(normalText);
		}
	}
	
	public void setLoadingText(String loadingText) {
		this.loadingText = loadingText;
		if(loading) {
			setText(loadingText);
		}
	}
	
	public String getLoadingText() {
		return loadingText;
	}
	
	public String getNormalText() {
		return normalText;
	}
	
	private void init() {
		setOnClickListener(this);
		setButtonOnClickListener(this);
		setText(normalText);
	}

	public void setOnProcessListener(OnProcessListener processListener) {
		this.processListener = processListener;
	}

	/**
	 * 按钮是否正处于加载状态
	 * @return
	 */
	public boolean isLoading() {
		return loading;
	}
	
	@Override
	public void onClick(View v) {
		doProcess();
	}

	public void doProcess() {
		if(!loading && processListener != null) {
			toggleProcess();
			if(processListener != null) {
				processListener.onProcess();
			}
		}
	}
	
	public void onComplete() {
		loading = false;
		progressBar.setVisibility(View.GONE);
		setText(normalText);
		setEnabled(true);
	}
	
	/**
	 * 切换到正在刷新状态
	 */
	protected void toggleProcess() {
		loading = true;
		progressBar.setVisibility(View.VISIBLE);
		setText(loadingText);
		setEnabled(false);
	}
	
	public interface OnProcessListener {
		/**
		 * 在按钮进入Loading状态后，将调用此方法。通常应该在这里实现加载业务。
		 * 在加载完成之后，调用{@link com.immomo.momo.android.view.LoadingButton#onComplete()} 方法，
		 * 以通知按钮回到正常状态。
		 */
		void onProcess();
	}
}
